import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class InputWidget extends StatefulWidget {
  const InputWidget(
      {super.key,
      this.hintText = '',
      this.onChanged,
      this.maxLength,
      this.maxLines = 1,
      this.showCount = false,
      this.borderRadius = 16,
      this.suffix,
      this.keyboardType = TextInputType.text,
      this.textInputAction = TextInputAction.done,
      this.height = 52});
  final String hintText;
  final ValueChanged? onChanged;
  final int? maxLength;
  final int? maxLines;
  final bool showCount;
  final double borderRadius;
  final Widget? suffix;
  final TextInputType keyboardType;
  final TextInputAction textInputAction;
  final double height;
  @override
  State<InputWidget> createState() => _InputWidgetState();
}

class _InputWidgetState extends State<InputWidget> {
  int _length = 0;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
            margin: const EdgeInsets.fromLTRB(0, 7, 0, 7),
            padding: widget.maxLines != null && widget.maxLines! > 1 ||
                    widget.maxLines == null
                ? const EdgeInsets.fromLTRB(17, 0, 13, 0)
                : const EdgeInsets.fromLTRB(17, 0, 13, 0),
            decoration: BoxDecoration(
              border: Border.all(
                color: const Color(0xFFAAAAAA),
              ),
              borderRadius: BorderRadius.circular(widget.borderRadius),
              color: const Color(0xFF3F3E44),
            ),
            child: Row(
              children: [
                Expanded(
                  flex: 1,
                  child: SizedBox(
                    height: widget.height,
                    child: TextField(
                      maxLength: widget.maxLength,
                      maxLines: widget.maxLines,
                      cursorHeight: 14.0,
                      cursorColor: const Color(0xFFFFFFFF),
                      keyboardType:
                          widget.textInputAction == TextInputAction.newline
                              ? TextInputType.multiline
                              : widget.keyboardType,
                      textInputAction: widget.textInputAction,
                      style: const TextStyle(
                        color: Color(0xFFFFFFFF),
                        height: 1.4,
                        fontSize: 15,
                      ),
                      decoration: InputDecoration(
                        counterText: '',
                        contentPadding: widget.maxLines == null
                            ? const EdgeInsets.fromLTRB(0, 17, 0, 17)
                            : const EdgeInsets.all(0),
                        filled: true,
                        fillColor: Colors.transparent,
                        border: InputBorder.none,
                        hintText: widget.hintText,
                        hintStyle: const TextStyle(
                          color: Color(0xFFCDCDCD),
                          fontSize: 14,
                          height: 1.4,
                          overflow: TextOverflow.clip,
                        ),
                      ),
                      onChanged: (value) {
                        widget.onChanged!(value);
                        setState(() {
                          _length = value.length;
                        });
                      },
                    ),
                  ),
                ),
                widget.suffix ?? const SizedBox(),
              ],
            )),
        Positioned(
          right: 10,
          bottom: 17,
          child: Offstage(
            offstage: !widget.showCount,
            child: Text(
              '$_length/200',
              style: const TextStyle(
                fontSize: 10,
                color: Color(0xFFCDCDCD),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
